package com.ktgj.service.service.impl;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.service.domain.ActivitycodebatchContinuation;
import com.ktgj.service.domain.ContractLinkActivitycode;
import com.ktgj.service.domain.KtserviceContractActivitycodebatch;
import com.ktgj.service.mapper.ActivitycodebatchContinuationMapper;
import com.ktgj.service.mapper.ContractLinkActivitycodeMapper;
import com.ktgj.service.mapper.KtserviceContractActivitycodebatchMapper;
import com.ktgj.service.service.IKtserviceContractActivitycodebatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 码批次			
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
@Transactional
public class KtserviceContractActivitycodebatchServiceImpl implements IKtserviceContractActivitycodebatchService 
{
    @Autowired
    private KtserviceContractActivitycodebatchMapper ktserviceContractActivitycodebatchMapper;

    //活动码续码
    @Autowired
    private ActivitycodebatchContinuationMapper activitycodebatchContinuationMapper;

    //活动码明细
    @Autowired
    private ContractLinkActivitycodeMapper contractLinkActivitycodeMapper;

    /**
     * 查询码批次			

     * 
     * @param activityCodeBatchId 码批次			
ID
     * @return 码批次			

     */
    @Override
    public KtserviceContractActivitycodebatch selectKtserviceContractActivitycodebatchById(Long activityCodeBatchId)
    {
        return ktserviceContractActivitycodebatchMapper.selectKtserviceContractActivitycodebatchById(activityCodeBatchId);
    }

    /**
     * 查询码批次			
列表
     * 
     * @param KtserviceContractActivitycodebatch 码批次			

     * @return 码批次			

     */
    @Override
    public List<KtserviceContractActivitycodebatch> selectKtserviceContractActivitycodebatchList(KtserviceContractActivitycodebatch KtserviceContractActivitycodebatch)
    {
        return ktserviceContractActivitycodebatchMapper.selectKtserviceContractActivitycodebatchList(KtserviceContractActivitycodebatch);
    }

    /**
     * 新增码批次			

     * 
     * @param KtserviceContractActivitycodebatch 码批次			

     * @return 结果
     */
    @Override
    public int insertKtserviceContractActivitycodebatch(KtserviceContractActivitycodebatch KtserviceContractActivitycodebatch)
    {
        KtserviceContractActivitycodebatch.setCreateTime(DateUtils.getNowDate());
        //码批次转为大写
        String bathName = KtserviceContractActivitycodebatch.getBatchName().toUpperCase().trim();
        KtserviceContractActivitycodebatch.setBatchName(bathName);
        int result = ktserviceContractActivitycodebatchMapper.insertKtserviceContractActivitycodebatch(KtserviceContractActivitycodebatch);
        if(result > 0){
            //获取新增后的活动码批次值
            Long activityCodeBatchId = KtserviceContractActivitycodebatch.getActivityCodeBatchId();
            bathInsert(KtserviceContractActivitycodebatch,activityCodeBatchId);
        }
        return result;
    }

    /**
     * 修改码批次			

     * 
     * @param KtserviceContractActivitycodebatch 码批次			

     * @return 结果
     */
    @Override
    public int updateKtserviceContractActivitycodebatch(KtserviceContractActivitycodebatch KtserviceContractActivitycodebatch)
    {
        KtserviceContractActivitycodebatch.setUpdateTime(DateUtils.getNowDate());
        return ktserviceContractActivitycodebatchMapper.updateKtserviceContractActivitycodebatch(KtserviceContractActivitycodebatch);
    }

    /**
     * 批量删除码批次			

     * 
     * @param activityCodeBatchIds 需要删除的码批次			
ID
     * @return 结果
     */
    @Override
    public int deleteKtserviceContractActivitycodebatchByIds(Long[] activityCodeBatchIds)
    {
        return ktserviceContractActivitycodebatchMapper.deleteKtserviceContractActivitycodebatchByIds(activityCodeBatchIds);
    }

    /**
     * 删除码批次			
信息
     * 
     * @param activityCodeBatchId 码批次			
ID
     * @return 结果
     */
    @Override
    public int deleteKtserviceContractActivitycodebatchById(Long activityCodeBatchId)
    {
        return ktserviceContractActivitycodebatchMapper.deleteKtserviceContractActivitycodebatchById(activityCodeBatchId);
    }

    /**
     * 活动码续码
     * @param bathName 码批次名称
     * @param generateNumber 续码个数
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Override
    public int addActivityCodeBathContinuation(String bathName, Long generateNumber,
                                               Date startDate, Date endDate) {
        int sum= 0;
        //先根据码批次名称获取对应的记录
        KtserviceContractActivitycodebatch activitycodebatch = ktserviceContractActivitycodebatchMapper.selectContractActivityCodeByBathName(bathName);
        if(activitycodebatch!=null){
            ActivitycodebatchContinuation continuation = new ActivitycodebatchContinuation();
            continuation.setEndDate(endDate);
            continuation.setStartDate(startDate);
            continuation.setGenerateNumber(generateNumber);
            continuation.setActivityCodeBatchId(activitycodebatch.getActivityCodeBatchId());
            continuation.setCreateTime(DateUtils.getNowDate());
            //保存活动码续表数据
            int result = activitycodebatchContinuationMapper.insertActivitycodebatchContinuation(continuation);
            if(result > 0){
                sum = bathInsertActivityCode(generateNumber,startDate,endDate,activitycodebatch);
            }
        }
        return sum;
    }

    /**
     * 批量操作活动明码明细 (续码表用)
     * @param generateNumber
     * @param startDate
     * @param endDate
     * @param activitycodebatch
     * @return
     */
    private int bathInsertActivityCode(Long generateNumber, Date startDate,
                                      Date endDate,KtserviceContractActivitycodebatch activitycodebatch){
        int result = 0 ;
        int count= generateNumber.intValue();
        for (int i = 0; i < count; i++) {
            ContractLinkActivitycode activitycode=new ContractLinkActivitycode();
            String batchName = activitycodebatch.getBatchName();
            //生成随机活动码
            String activityCode = batchName + getRandomChar(6);
            activitycode.setActivityCode(activityCode);
            activitycode.setActivityCodeBatchId(activitycodebatch.getActivityCodeBatchId());
            activitycode.setStartDate(startDate);
            activitycode.setEndDate(endDate);
            activitycode.setSupportBussenissTypeList(activitycodebatch.getSupportBussenissTypeList());
            activitycode.setStatus("0");
            activitycode.setCreateTime(DateUtils.getNowDate());
            //保存活动码明细
            result = contractLinkActivitycodeMapper.insertContractLinkActivitycode(activitycode);
            result ++;
        }
        return result;
    }

    /**
     * 批量操作活动码明细 (添加活动码使用)
     * @param KtserviceContractActivitycodebatch
     * @return
     */
    private int bathInsert(KtserviceContractActivitycodebatch KtserviceContractActivitycodebatch,Long activityCodeBatchId){
        //生成个数
        int count= KtserviceContractActivitycodebatch.getGenerateNumber().intValue();
        int result=0;
        for (int i = 0; i < count; i++) {
            ContractLinkActivitycode activitycode=new ContractLinkActivitycode();
            //码批次转为大写
            String bathName = KtserviceContractActivitycodebatch.getBatchName().toUpperCase().trim();
            //生成随机活动码
            String activityCode = bathName + getRandomChar(6);
            activitycode.setActivityCode(activityCode);
            activitycode.setActivityCodeBatchId(activityCodeBatchId);
            activitycode.setStartDate(KtserviceContractActivitycodebatch.getStartDate());
            activitycode.setEndDate(KtserviceContractActivitycodebatch.getEndDate());
            activitycode.setSupportBussenissTypeList(KtserviceContractActivitycodebatch.getSupportBussenissTypeList());
            activitycode.setStatus("0");
            activitycode.setCreateTime(DateUtils.getNowDate());
            result = contractLinkActivitycodeMapper.insertContractLinkActivitycode(activitycode);
            result ++;
        }

        return result;
    }

    /**
     * 获得0-9,a-z范围的随机数
     * @param length 随机数长度
     * @return
     */
    private String getRandomChar(int length){
        char[] chr = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f','g','h','i','j','k','l',
                'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }


}
